package com.jiang.daily.Q1706;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/02/15 20:57
 */
class Solution {
    public int[] findBall(int[][] grid) {
        int n = grid[0].length;
        int[] res = new int[n];
        for (int j = 0; j < n; j++) {
            int col = j;
            for (int[] row: grid) {
                int dir = row[col]; // 方向
                col += dir; // 球下一个到达的列
                if (col < 0 || col == n || row[col] != dir) {
                    col = -1;
                    break;
                }
            }
            res[j] = col;
        }
        return res;
    }
}
